
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer
from mall import settings


# 抽取单独功能　封装成函数
def make_openid(openid):
    # 创建序列化器　第一参数　默认使用配置中秘钥　进行加密　第二参数是时间　过期时间
    serializer = Serializer(secret_key=settings.SECRET_KEY,expires_in=3600)

    data = {
        'openid':openid
    }
    # 进行加密　
    tokenid = serializer.dumps(data)
    # 生成的byte类型　转成字符串
    return tokenid.decode()


def check_token(token):

    serializer = Serializer(secret_key=settings.SECRET_KEY,expires_in=3600)

    try:
        openid = serializer.loads(token)
    except Exception:
        return None

    return openid